/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.app.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yanlight.app.dal.dao.CompanyDAO;
import com.yanlight.app.dal.dao.DeveloperDAO;
import com.yanlight.app.dal.dto.DeveloperQuery;
import com.yanlight.app.dal.entity.Company;
import com.yanlight.app.dal.entity.Developer;
import com.yanlight.app.model.DeveloperSpec;
import com.yanlight.app.request.DeveloperAddRequest;
import com.yanlight.app.request.DeveloperModifyRequest;
import com.yanlight.app.request.DeveloperQueryRequest;
import com.yanlight.app.response.DeveloperQueryResponse;
import com.yanlight.app.service.DeveloperService;
import com.yanlight.type.BooleanType;
import com.yanlight.util.DateTimeUtil;
import com.yanlight.util.EncryptUtil;
import com.yanlight.util.JedisUtil;
import com.yanlight.util.ToolsUtil;

/**
 * 
 * @author haolw
 * @time 2018/03/02
 */
@Service
public class DeveloperServiceImpl implements DeveloperService {
	public final String DEV_TOKEN_CACHE_PREFIX = "dev:token:";
	@Autowired
	private DeveloperDAO developerDAO;
	@Autowired
	private CompanyDAO companyDAO;

	@Override
	public DeveloperQueryResponse findList(DeveloperQueryRequest request) {
		DeveloperQueryResponse response = new DeveloperQueryResponse();
		DeveloperQuery condition = new DeveloperQuery();
		BeanUtils.copyProperties(request, condition);
		int count = developerDAO.count(condition);
		if (count > 0) {
			List<Developer> developers = developerDAO.findList(condition);
			List<Integer> companyIds = new ArrayList<Integer>(developers.size());
			for (Developer developer : developers) {
				companyIds.add(developer.getCompanyId());
			}
			List<Company> companys = companyDAO.findByIds(companyIds);
			Map<Integer, Company> companyMap = new HashMap<Integer, Company>(companys.size());
			for (Company company : companys) {
				companyMap.put(company.getId(), company);
			}
			DeveloperSpec spec = null;
			for (Developer developer : developers) {
				spec = new DeveloperSpec();
				BeanUtils.copyProperties(developer, spec);
				Company company = companyMap.get(developer.getCompanyId());
				spec.setCompanyName(company.getName());
				response.addSpec(spec);
			}
		}
		response.setTotalRecord(count);
		response.setPageSize(request.getPageSize());
		return response;
	}

	@Override
	public void insert(DeveloperAddRequest request) {
		Developer developer = new Developer();
		BeanUtils.copyProperties(request, developer);
		String token = ToolsUtil.uuid();
		String secret = EncryptUtil.md5_16(ToolsUtil.uuid());
		developer.setToken(token);
		developer.setSecret(secret);
		developer.setCreateTime(DateTimeUtil.getNowTime());
		developerDAO.save(developer);
		JedisUtil.set(getDevTokenKey(token), developer);
	}

	@Override
	public void modify(DeveloperModifyRequest request) {
		Integer id = request.getId();
		Developer developer = developerDAO.findById(id);
		if (developer != null) {
			Integer status = request.getStatus();
			Integer secretMark = request.getSecret();
			if (status != null) {
				developer.setStatus(status);
			}
			if (BooleanType.TRUE.ordinal() == secretMark.intValue()) {
				String secret = EncryptUtil.md5_16(ToolsUtil.uuid());
				developer.setSecret(secret);
			}
			developerDAO.update(developer);
			String token = developer.getToken();
			JedisUtil.set(getDevTokenKey(token), developer);
		}
	}

	private String getDevTokenKey(String token) {
		return DEV_TOKEN_CACHE_PREFIX.concat(token);
	}
}
